package com.fas.system.service.impl;

import com.fas.common.core.domain.AjaxResult;
import com.fas.common.core.redis.RedisCache;
import com.fas.system.domain.FasFireEngine;
import com.fas.system.mapper.FasFireEngineMapper;
import com.fas.system.service.IFasFireEngineService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FasFireEngineServiceImpl implements IFasFireEngineService {

    @Resource
    private FasFireEngineMapper fasFireEngineMapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<FasFireEngine> findAllEquipment(FasFireEngine fasFireEngine) {
        List<FasFireEngine> list = fasFireEngineMapper.findAllEquipment(fasFireEngine);
        list.forEach(ll -> ll.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(ll.getCooperationCompanyId())));
        return list;
    }

    @Override
    public List<FasFireEngine> findLikeEngineer(FasFireEngine fasFireEngine) {
        List<FasFireEngine> list = fasFireEngineMapper.findLikeEngineer(fasFireEngine);
        list.forEach(ll -> ll.setCompanyName(FasCooperationCompanyServiceImpl.COMPANY_NAMES.get(ll.getCooperationCompanyId())));
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertFasFireEngine(FasFireEngine fasFireEngine) {
        String deviceCode = fasFireEngine.getDeviceCode();
        int exists = fasFireEngineMapper.existsDeviceCode(deviceCode);
        if (exists > 0) {
            return AjaxResult.error("设备编号已存在，请重新输入！");
        }
        return fasFireEngineMapper.insertFasFireEngine(fasFireEngine) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateFasFireEngine(FasFireEngine fasFireEngine) {
        int exists = fasFireEngineMapper.existsDeviceCodeById(fasFireEngine);
        if (exists > 0) {
            return AjaxResult.error("设备编号已存在，请重新输入！");
        }
        return fasFireEngineMapper.updateFasFireEngine(fasFireEngine) > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteFasFireEngine(Integer[] ids) {
        return fasFireEngineMapper.deleteFasFireEngine(ids);
    }
}
